$(function(){
	$("#publishBtn").click(publish);
});

function publish() {
	$("#publishModal").modal("hide");

    let title = $("#recipient-name").val();
    let content = $("#message-text").val();

    // 发送AJAX前，带上令牌
    let token = $("meta[name= '_csrf']").attr("content");
    let header = $("meta[name= '_csrf_header']").attr("content");
    $(document).ajaxSend(function (e, xhr, options){
        xhr.setRequestHeader(header, token);
    });

    $.post(
        CONTEXT_PATH + "/discuss/add",
        {"title":title,"content":content},
        function(data){
            data = $.parseJSON(data);
            // 提示框中显示返回信息
            $("#hintBody").text(data.message);
            $("#hintModal").modal("show");
            setTimeout(function (){
                $("#hintModal").modal("hide");
                if(data.code === 0){
                    // 发布成功后，刷新页面
                    window.location.reload();
                }
                // 清空输入框
                $("#recipient-name").val("");
                $("#message-text").val("");
            }, 2000);
        }
    )

}
